/**
 * create by young (young2f@outlook.com) on 2020/12/3
 */
'use strict';
const base = require('../base/controllerBase');

class RoleController extends base {
    async createRole() {
        const {ctx} = this;
        try {
            const {body} = ctx.request;
            console.log('createRole body', body);
            const result = await ctx.app.dbs.insert('Role', body);
            const status = result ? 'ok' : 'error';
            ctx.body = {status, result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findRoles() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {tenantCode: {type: 'string'}};
            ctx.validate(model, body);
            const result = await ctx.app.dbs.find('Role', body);
            ctx.body = {status: 'ok', result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findRole() {
        const {ctx} = this;
        try {
            const {body} = ctx.request;
            const model = {tenantCode: {type: 'string'}, roleCode: {type: 'string'}};
            ctx.validate(model, body);
            const result = await ctx.app.dbs.findOne('Role', body);
            ctx.body = {status: 'ok', result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    /**
     * force:是否强制删除
     * @returns {Promise<void>}
     */
    async deleteRole() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {tenantCode: {type: 'string'}, roleCode: {type: 'string'}};

            ctx.validate(model, body);
            const {tenantCode, roleCode} = body;
            const result = await ctx.app.dbs.deleteOne('Role', body);
            ctx.body = {status: 'ok', result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async updateRole() {
        const {ctx} = this;
        try {
            const {body} = ctx.request;
            const model = {tenantCode: {type: 'string'}, roleCode: {type: 'string'}};
            ctx.validate(model, body);
            const {tenantCode, roleCode} = body;
            const result = await ctx.app.dbs.updateOne('Role', {tenantCode, roleCode}, body);
            const status = result ? 'ok' : 'error';
            ctx.body = {status, result};
            ctx.status = 200;
        } catch (e) {
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }
}

module.exports = RoleController;
